package client.comm.baoding.ui

import android.view.View
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.NoticeAdapter
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.TxJl
import client.comm.baoding.databinding.ActivityNoticeBinding
import client.comm.baoding.ui.vm.NoticeViewModel
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.widget.EmptyLayout

/**
 * 公告
 */
class NoticeActivity : BaseMiddleActivity<NoticeViewModel, ActivityNoticeBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_notice
    }

    val adapter: NoticeAdapter by lazy {
        NoticeAdapter(this, mImgLoader)
    }

    override fun initView() {
        binding?.apply {
            event = this@NoticeActivity
            vm = viewModel
            recyclerView.adapter = adapter
            recyclerView.addItemDecoration(SpaceItemDecoration(10))
            emptyLayout.setOnClickListener { viewModel.holdOne() }
            refreshLayout.setOnRefreshListener {

                adapter.submitList(articleList)
                binding.emptyLayout.visibility = View.GONE
                adapter.hasMoreState.value = false
                refreshLayout.isRefreshing=false
//                viewModel.pageOne.value = 0
//                viewModel.holdOne()
            }
            recyclerView.onLoadMore = {
                adapter.hasMoreState.value = false
                /*if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
//                    viewModel.holdOne()
                }*/
            }
        }
    }

    override fun observerUI() {
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })


//        viewModel.callOne.observe(this, {
//
//        })


        articleList.let {
            if (it != null && it.size > 0) {
                adapter.submitList(it)
                binding.emptyLayout.visibility = View.GONE
                adapter.hasMoreState.value = false
//                if (viewModel.pageOne.value == 1) {
//
//                } else
//                    adapter.appendList(list)
//                if (list != null && list.size > 0) {
//                    if (list.size < 10) {
//                        adapter.hasMoreState.value = false
//                    }
//                    binding.emptyLayout.visibility = View.GONE
//                } else {
//                    if (adapter.itemCount == 0)
//                        binding.emptyLayout.errorType = EmptyLayout.NODATA
//                    adapter.hasMoreState.value = false
//                }


            } else {
                if (adapter.itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
            }
        }

    }

    var articleList: ArrayList<GoodsIndex.Article>? = null;

    override fun initData() {
        viewModel.type.value = "${intent.getIntExtra("type", 1)}"
        articleList = intent.getParcelableArrayListExtra<GoodsIndex.Article>("articleList")

//        viewModel.holdOne()

//        //todo 以下为测试数据
//        val mList2: ArrayList<TxJl.Withdraw> = arrayListOf()
//        mList2.add(TxJl.Withdraw("",0,"0","0",0,0,0,0,"0"))
//        mList2.add(TxJl.Withdraw("",0,"0","0",0,0,0,0,"0"))

        adapter.submitList(articleList)
        binding.emptyLayout.visibility = View.GONE
        adapter.hasMoreState.value = false

    }

    override fun initViewModel(): NoticeViewModel = createViewModel()

}